<?php
/**
 * Summary
 * 
 * Adaptador a Memcached usando la extensión de Php5 Memchached
 */
namespace SubMemory\Cache;

use Previsora\AuthPrv\Cache\CacheInterface;

/**
 * Adaptador Memcached
 */
class MemcachedCache extends \Memcached implements CacheInterface
{
    /**
     * Arreglo de servidores
     * @var array
     */
    protected $_servers = array();
    
    /**
     * Constructor 
     * @param array $servers
     */
    public function __construct($servers = '')
    {
       	parent::__construct();
        $ss = $this->getServerList ();
        if (empty ( $ss )) {
		$this->setOption(\Memcached::OPT_COMPRESSION, false);
		$this->setOption(\Memcached::OPT_RECV_TIMEOUT, 1000);
		$this->setOption(\Memcached::OPT_SEND_TIMEOUT, 1000);
		$this->setOption(\Memcached::OPT_TCP_NODELAY, true);
		$this->setOption(\Memcached::OPT_SERVER_FAILURE_LIMIT, 50);
		$this->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 500);
		$this->setOption(\Memcached::OPT_RETRY_TIMEOUT, 300);
		$this->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT);
		//$this->setOption(\Memcached::OPT_REMOVE_FAILED_SERVERS, true);
		$this->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);

		if(!$servers)
		{
			$this->addServer( "localhost", 11211, 1 );
                }else {
                    
                    foreach ($servers as $server) {
                        $this->addServer ( $server, 11211, 1 );
                    }
                }
                
		
	}
    }//end __construct
    
    /**
     * Destructor
     */
    public function __destruct() {
        parent::quit();
        $this->_servers = array();
    }
    /**
     * set Añade un nuevo ítem asociado a una nueva clave o lo modifica si existe.
     * @param string $key
     * @param string $value
     * @param string $expirationtime
     */
    public function set($key, $value, $expirationtime=0) {
        //if(is_null($options)) $options = 3600;//expirationtime
        parent::set($key, $value, $expirationtime);
    }
    /**
     * get Obtener un ítem
     * @param string $key
     * @param string $cache_cb
     * @param string $cas_token
     * @return string
     */
    public function get($key,$cache_cb = null, &$cas_token = null){
        return parent::get($key);
    }
    /**
     * delete  Eliminar un ítem
     * @param string $key
     * @param int $time
     */
    public function delete($key, $time =0)
    {
        parent::delete($key);
    } 
    
     /**
     * Cerrar la conexión al cache
     */
    public function close(){
        return true;
    }
    
    /**
     * Colector de basura.
     * @param string $maxlifetime
     */
    public function gc($maxlifetime){
        return true;
    }
}

?>
